﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.IO.IsolatedStorage;
using SQLiteClient;

namespace Vanglen.YDLMSCP
{
    public partial class DetailList : PhoneApplicationPage
    {
        public DetailList()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

            //SQLiteConnection db = new SQLiteConnection("yidalimeishi.db");
            //db.Open();
            //SQLiteCommand cmd = db.CreateCommand(" select * from caipu ");
            //object obj = cmd.ExecuteScalar();

            //db.Dispose();
            //db = null;
            IDictionary<string, string> parameters = this.NavigationContext.QueryString;

            if (parameters.ContainsKey("d"))
            {
                string strC = parameters["d"];
                if (!String.IsNullOrEmpty(strC))
                    pivot1.SelectedIndex = Int32.Parse(strC);
            }
        }

        private void tbFilter_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                BindSearch();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void appbar_button1_Click(object sender, EventArgs e)
        {
            try
            {
                BindPivot();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                BindSearch();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void pivot1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                BindPivot();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void pList_Tap(object sender, GestureEventArgs e)
        {
            ListBox lb = (ListBox)sender;
            if (lb.SelectedIndex > -1)
                NavigationService.Navigate(new Uri(String.Format("/Detail.xaml?id={0}", (lb.SelectedValue as Caipu)._id), UriKind.Relative));
        }

        private void BindPivot()
        {
            switch (pivot1.SelectedIndex)
            {
                //case 0://查询
                //    BindSearch();
                //    break;
                //case 1://所有
                //    BindAll();
                //    break;
                //case 2://已收藏
                //    BindFavirate();
                //    break;
                //case 3://已查看
                //    BindViewed();
                //    break;
                //case 4://随机
                //    BindRandom();
                //    break;

                case 0://所有
                    BindAll();
                    break;
                case 1://已收藏
                    BindFavirate();
                    break;
                case 2://已查看
                    BindViewed();
                    break;
                case 3://随机
                    BindRandom();
                    break;
            }
        }

        private void BindSearch()
        {
            //if (!String.IsNullOrEmpty(tbFilter.Text))
            //{
            //    SQLiteHelper helper = new SQLiteHelper();
            //    string sql = String.Format("select * from caipu where [name] like '%{0}%'", tbFilter.Text);
            //    var result = helper.ExecuteQuery<Caipu>(sql);
            //    lbSearch.ItemsSource = result.ToList();
            //    helper.Close();
            //}
            //else
            //{
            //    lbSearch.ItemsSource = null;
            //}
        }

        private void BindAll()
        {
            using (SQLiteHelper helper = new SQLiteHelper())
            {
                string sql = " select * from caipu ";
                var result = helper.ExecuteQuery<Caipu>(sql);
                lbAll.ItemsSource = result.ToList();
            }
            //SQLiteConnection db = new SQLiteConnection("yidalimeishi.db");
            //db.Open();
            //SQLiteCommand cmd = db.CreateCommand(" select * from caipu ");
            //object obj = cmd.ExecuteScalar();

            //db.Dispose();
            //db = null;
        }

        private void BindFavirate()
        {
            using (SQLiteHelper helper = new SQLiteHelper())
            {
                string sql = "select * from caipu where favorite = 1";
                var result = helper.ExecuteQuery<Caipu>(sql);
                lbFavorite.ItemsSource = result.ToList();
            }
        }

        private void BindViewed()
        {
            using (SQLiteHelper helper = new SQLiteHelper())
            {
                string sql = "select * from caipu where history = 1";
                var result = helper.ExecuteQuery<Caipu>(sql);
                lbViewed.ItemsSource = result.ToList();
            }
        }

        private void BindRandom()
        {
            using (SQLiteHelper helper = new SQLiteHelper())
            {
                string sql = "select * from caipu order by RANDOM() limit 10";
                var result = helper.ExecuteQuery<Caipu>(sql);
                lbRandom.ItemsSource = result.ToList();
            }
        }
    }
}